<?php

declare(strict_types=1);

namespace App\Controller;

use App\Model\Test;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;

class IndexController extends AbstractController
{

    public function returnJson(int $code, array $data, string $msg = 'success'):Psr7ResponseInterface
    {
        $data = [
            'code' => $code,
            'data' => $data,
            'msg'  => $msg,
        ];
        return $this->response->json($data);
    }

    public function text():Psr7ResponseInterface
    {
        return $this->returnJson(1, [], 'text');
    }

    public function sleep(): Psr7ResponseInterface
    {
        //处理程序要1秒
        sleep(1);
        return $this->returnJson(1, [], 'sleep');
    }

    public function redisSet(): Psr7ResponseInterface
    {
        $key       = 'test_key';
        //$container = ApplicationContext::getContainer();
        $redis     = $this->container->get(\Hyperf\Redis\Redis::class);
        $result    = $redis->set($key, time());
        return $this->returnJson(1, [], 'redisSet');
    }

    public function redisGet(): Psr7ResponseInterface
    {
        $key       = 'test_key';
        //$container = ApplicationContext::getContainer();
        $redis     = $this->container->get(\Hyperf\Redis\Redis::class);
        $ret       = $redis->get($key);
        return $this->returnJson(1, [$ret], 'redisGet');
    }

    public function mysqlCreate(): Psr7ResponseInterface
    {
        $data  = [
            'name'     => 'lzj_' . time(),
            'password' => md5('123456'),
            'sex'      => 1,
        ];
        $model = Test::create($data);
        return $this->returnJson(1, [], 'mysqlCreate');
    }

    public function mysqlShow(): Psr7ResponseInterface
    {
        $ret = Test::query()->find(1);
        return $this->returnJson(1, [], 'mysqlShow');
    }

}
